﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace proyecto
{
    public partial class Form1 : Form
    {
        public cone co; //objeto para la conexion
        public string conexion = "";//cadena de conexion
        public string[] proce = new string[2];
        int pro = 0;



        public Form1()
        {
            fecha_clase fecha_cla;
            InitializeComponent();
            co = new cone();
            Ini();
            inicializar();
            fecha_cla = new fecha_clase();
            txtfecha.Text = DateTime.Now.ToShortDateString();
            txtfecha.Text = fecha_cla.cam_fecha(txtfecha.Text);
        }
        private void Ini()
        {
            try
            {
                co.Crear_Cadena_Conexion1("localhost", "remolques", "root", "root");
                conexion = co.Prueba_Conexion1();
            }
            catch { }
        }

        private void SetupDataGridView(int ngh)
        {
            this.Controls.Add(dataGridView2);
            dataGridView2.ColumnCount = pro + 1;

            dataGridView2.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dataGridView2.RowHeadersVisible = false;

            for (int i = 0; i <= pro; i++)
            {
                dataGridView2.Columns[i].Name = proce[i];
            }
            for (int i = 0; i <= pro; i++)
            {
                dataGridView2.Columns[i].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            }
            for (int i = 0; i <= pro; i++)
            {
                if (i == ngh)
                {
                    dataGridView2.Columns[i].DefaultCellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                }
                else
                {
                    dataGridView2.Columns[i].DefaultCellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                }
            }
        }
        private void PopulateDataGridView()
        {
            for (int i = 0; i <= pro; i++)
            {
                dataGridView2.Columns[i].DisplayIndex = i;
            }

            for (int i = 0; i <= pro; i++)
            {
                dataGridView2.Columns["" + proce[i]].ReadOnly = true;
            }


            this.dataGridView2.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
        }
        private void songsDataGridView_CellFormatting(object sender,
        System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
        {
            if (this.dataGridView2.Columns[e.ColumnIndex].Name == "")
            {
                if (e != null)
                {
                    if (e.Value != null)
                    {
                        try
                        {
                            e.Value = DateTime.Parse(e.Value.ToString())
                                .ToLongDateString();
                            e.FormattingApplied = true;
                        }
                        catch (FormatException)
                        {
                            Console.WriteLine("{0} is not a valid date.", e.Value.ToString());
                        }
                    }
                }
            }
        }


        private void inicializar()
        {
            pro = 0;
            string str = "";
            for (int i = 0; i < 130; i++)
                str += " ";

            proce[0] = "" + str + "NOMBRE";
            proce[1] = "CANTIDAD";
            pro = 1;
            SetupDataGridView(0);
            PopulateDataGridView();
            dataGridView2.Columns[0].Width = 760;
            dataGridView2.Rows[0].Cells[0].Selected = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void txtfecha_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        public string buscar(string codi)
        {
            string cad = "";
            try
            {
                DataTable tabla1 = new DataTable();
                MySqlConnection mysql_conexion1 = new MySqlConnection(conexion);
                String query_selecionar = "SELECT nombre,precio FROM `jugueria`.`articulos` WHERE codigo= '" + codi + "'";
                MySqlDataAdapter mysql_data = new MySqlDataAdapter(query_selecionar, mysql_conexion1);
                mysql_conexion1.Open();
                mysql_data.Fill(tabla1);
                cad = (string)tabla1.Rows[0][0];
          
                mysql_conexion1.Close();
            }
            catch
            {
                cad = " ";
                MessageBox.Show("Error artículo no existe", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtcodigo.Text = "";
                txtcodigo.Focus();
            }
            return cad;
        }

        private void txtcodigo_KeyDown(object sender, KeyEventArgs e)
        {

        }
    }
}
